﻿require 'tk'
main=TkRoot.new {title 'игра "Виселица"'}

$slovo = TkVariable.new
$slovo_txt = TkVariable.new
$c = TkVariable.new
$life = TkVariable.new
$charArr = TkVariable.new
$msg = TkVariable.new
$found = TkVariable.new

def getWord()
	myArr = ['mother','father','brother','sister','son','daughter']
	item = myArr[rand(myArr.length)]
	return item
end 

def setWord
	$slovo.value = getWord()
	$life.value = 3
	$charArr.value = ''
	$msg.value = ''
	$found.value = 0
	
	setMaskedWord()
end

def setMaskedWord()
	word = $slovo.value
	charArr = $charArr.value
	result = ''
	j = 0
	while j < word.size
		c = word[j]
		# если с в массиве charArr, тогда печатай с, иначе _
		if charArr.include? c
			result += word[j] 
		else
			result += ' _ '
		end
		j+=1
	end
	$slovo_txt.value = result
end

def restartCommand
	#puts 'Restart the word'
	setWord()
end

def getCharacter()
	return $c.value[0]
end

def findIndeces(_word,_character)
	i=0 # индек буквы == 0; 
	# цикл по индексу
	result = []
	while i <=  _word.size # пока индекс буквы <= длины строки
        if _character == _word[i]
			result.push(i)
		end
		i+=1 # сдвиг на 1 больше
	end
	
	return result
end

def checkCommand
	# ask for character
	c = getCharacter()
	# check character 
	# esli bukva uje v charArr zaprosit eshe raz
	# if abc contains c ask new character
	$c.value = ''
	if c.nil? or $charArr.value.include? c
		$msg.value = 'Вы уже вводили эту букву!'
		return
	end

	state = findIndeces($slovo.value, c)
	# if found increase found characters
	if 0 < state.length
		$found.value = $found.value.to_i + state.length
	else # if not fount decrease life
		$life.value = $life.value.to_i - 1
		$msg.value = 'Вы ошиблись! Осталось попыток: '
		$msg.value += $life.value
	end
	# save character to abc
	$charArr.value += c
	setMaskedWord()
	
	if $life.value.to_i == 0
		$msg.value = 'К сожалению, Bы проиграли! А слово-то было: ' + $slovo.value
	end
	if $found.value.to_i == $slovo.value.size
		$msg.value = 'Победа!' 
	end

end

Lb1 = TkLabel.new(main) do
	text 'Угадайте слово с 3 попыток. Иначе вас повесят! :)'
	borderwidth 5
	font TkFont.new('times 15 bold')
	relief 'groove'
	foreground  'dark green'
	pack { padx 15 ; pady 15; side 'left'}
end

Lb2 = TkLabel.new(main) do
	textvariable
	borderwidth 5
	font TkFont.new('times 15 bold')
	relief 'groove'
	foreground  'dark red'
	pack { padx 15 ; pady 15; side 'left'}
end
Lb2['textvariable'] = $slovo_txt

Lb3 = TkLabel.new(main) do
	textvariable
	borderwidth 5
	font TkFont.new('times 15 bold')
	relief 'groove'
	foreground  'dark red'
	pack { padx 15 ; pady 15; side 'left'}
end

Lb3['textvariable'] = $msg


b1 = TkButton.new(main) do 
	text 'Запуск'
	font('times 20 bold')
	foreground  "yellow"
	pack("side" => "right","anchor" => "n")
	command (proc{restartCommand})
end

b2 = TkButton.new(main) do 
	text 'Ввод'
	font('times 20 bold')
	foreground  "green"
	pack("side" => "right","anchor" => "ne")
	command (proc{checkCommand})
end


textfield = TkEntry.new(main) do
	pack { padx 8 ; pady 15; side 'left'}
	width 10
end
textfield.textvariable = $c

setWord()
main.mainloop